﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using System.Web.UI.HtmlControls;
using Yipusoft.BaseDoc;

public partial class Task_BaseDoc_DocReporter : System.Web.UI.Page
{
    BaseDocDataContext db = new BaseDocDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        BindDateList();
        BindTable();
    }

    private void BindDateList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            DptYear.Items.Add(li);
        }
        for (int i = 1; i <= 12; i++)
        {
            this.DptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        if (Request.QueryString["year"] == null)
        {
            DptMonth.SelectedValue = DateTime.Now.Month.ToString();
            DptYear.SelectedValue = DateTime.Now.Year.ToString();
        }
        else
        {
            DptMonth.SelectedValue = Request["month"];
            DptYear.SelectedValue = Request["year"];
        }
    }

    void BindTable()
    {
        //部门
        List<DepartmentInfo> Departments = Department.GetList(" Left(departmentid,6)='" + Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 6) + "' ");

        List<BaseDocType> types = (from pp in db.BaseDocType where pp.ParentID == 0 select pp).ToList();
        //头
        HtmlTableRow tr = new HtmlTableRow();
        tr.ID = "r" + Guid.NewGuid().ToString();


        HtmlTableCell td = new HtmlTableCell();
        td.ID = "d" + Guid.NewGuid().ToString();
        td.InnerText = "处室";
        td.Attributes.Add("class", "td");
        td.NoWrap = true;
        td.Width = "20%";
        tr.Cells.Add(td);

        td = new HtmlTableCell();
        td.ID = "d" + Guid.NewGuid().ToString();
        td.InnerText = "档案类型";
        td.Width = "10%";
        td.Attributes.Add("class", "td");
        tr.Cells.Add(td);

        td = new HtmlTableCell();
        td.ID = "d" + Guid.NewGuid().ToString();
        td.InnerText = "档案";
        td.Attributes.Add("class", "td");
        tr.Cells.Add(td);

        tb.Rows.Add(tr);

        //中
        for (int i = 0; i < Departments.Count; i++)
        {
            //此部门没有档案不显示
            if (!isHaveDoc(Departments[i].DepartmentID))
            {
                continue;
            }


            int departmentcount = 0;
            HtmlTableCell Departmenttd = new HtmlTableCell();
            Departmenttd.Attributes.Add("class", "td");
            Departmenttd.ID = "d" + Guid.NewGuid().ToString();
            Departmenttd.InnerText = Departments[i].ShortName;
            //所有的类型
            for (int j = 0; j < types.Count; j++)
            {
                int typeCount = 0;
                HtmlTableCell typetd = new HtmlTableCell();
                typetd.ID = "d" + Guid.NewGuid().ToString();
                typetd.InnerText = types[j].TypeName;
                typetd.Attributes.Add("class", "td");

                //下一类型
                List<BaseDocType> types2 = db.BaseDocType.Where(o => o.ParentID == types[j].ID).ToList();
                if (types2.Count > 0)//有下一类型是工作反馈
                {

                    for (int n = 0; n < types2.Count; n++)
                    {
                        tr = new HtmlTableRow();
                        tr.ID = "r" + Guid.NewGuid().ToString();

                        if (departmentcount == 0)
                        {
                            tr.Cells.Add(Departmenttd);
                        }
                        if (typeCount == 0)
                        {
                            tr.Cells.Add(typetd);
                        }


                        td = new HtmlTableCell();
                        td.ID = "d" + Guid.NewGuid().ToString();
                        //td.InnerText = types2[n].TypeName;

                        td.InnerHtml = types2[n].TypeName + ":";
                        List<BaseDocs> docs = (from p in db.BaseDocs where p.TID == types2[n].ID && p.SetDepartment == Departments[i].DepartmentID && p.CreateDate.Value.Year.ToString() == DptYear.SelectedValue && p.CreateDate.Value.Month.ToString() == DptMonth.SelectedValue select p).ToList();
                        for (int m = 0; m < docs.Count; m++)
                        {
                            td.InnerHtml += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a target='_blank' href='" + docs[m].Contents + "'>" + Convert.ToString(m + 1) + "、" + docs[m].Caption + "</a>";
                        }
                        td.Attributes.Add("class", "td1");

                        tr.Cells.Add(td);

                        departmentcount++;
                        typeCount++;

                        tb.Rows.Add(tr);
                    }

                }
                else
                {//没有下一类型
                    List<BaseDocs> docs = db.BaseDocs.Where(o => o.SetDepartment == Departments[i].DepartmentID && o.TID == types[j].ID && o.CreateDate.Value.Year == Convert.ToInt32(DptYear.SelectedValue) && o.CreateDate.Value.Month == Convert.ToInt32(DptMonth.SelectedValue)).ToList();
                    if (types[j].ID == 1)
                    {
                        if (docs.Count > 0)//有没有档案
                        {
                            for (int n = 0; n < docs.Count; n++)
                            {
                                tr = new HtmlTableRow();
                                tr.ID = "r" + Guid.NewGuid().ToString();

                                if (departmentcount == 0)
                                {
                                    tr.Cells.Add(Departmenttd);
                                }
                                if (typeCount == 0)
                                {
                                    tr.Cells.Add(typetd);
                                }

                                td = new HtmlTableCell();
                                td.ID = "d" + Guid.NewGuid().ToString();

                                td.InnerHtml = "<a href='DocView.aspx?ID=" + docs[n].ID + "'>" +Convert.ToString(n+1)+"、"+ docs[n].Caption + "</a>";


                                td.Attributes.Add("class", "td1");

                                tr.Cells.Add(td);

                                departmentcount++;
                                typeCount++;
                                tb.Rows.Add(tr);
                            }
                        }
                        else//没有档案
                        {
                            tr = new HtmlTableRow();
                            tr.ID = "r" + Guid.NewGuid().ToString();
                            if (departmentcount == 0)
                            {
                                tr.Cells.Add(Departmenttd);
                            }
                            if (typeCount == 0)
                            {
                                tr.Cells.Add(typetd);
                            }
                            td = new HtmlTableCell();
                            td.ID = "d" + Guid.NewGuid().ToString();
                            td.InnerHtml = "&nbsp;";
                            td.Attributes.Add("class", "td1");
                            tr.Cells.Add(td);

                            departmentcount++;
                            typeCount++;

                            tb.Rows.Add(tr);
                        }
                    }
                    else if (types[j].ID == 2)//日常工作
                    {

                        tr = new HtmlTableRow();
                        tr.ID = "r" + Guid.NewGuid().ToString();
                        if (departmentcount == 0)
                        {
                            tr.Cells.Add(Departmenttd);
                        }
                        if (typeCount == 0)
                        {
                            tr.Cells.Add(typetd);
                        }
                        td = new HtmlTableCell();
                        td.ID = "d" + Guid.NewGuid().ToString();
                        if (docs.Count > 0)
                        {
                            int n = 0;
                            foreach (BaseDocs doc in docs)
                            {
                                BaseDocFiles file = doc.BaseDocFiles.FirstOrDefault();
                                if (file != null)
                                {
                                    n++;
                                    td.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;<a target='_blank' href='../../Document/" + doc.SetDepartment + "/" + file.FileName + "' >" +n+"、"+ doc.Caption + "</a><br/>";
                                }
                                else
                                {
                                    td.InnerHtml += "";

                                }
                            }
                        }
                        else
                        {
                            td.InnerHtml = "&nbsp;";
                        }

                        td.Attributes.Add("class", "td1");

                        tr.Cells.Add(td);

                        departmentcount++;
                        typeCount++;

                        tb.Rows.Add(tr);

                    }
                }

                typetd.RowSpan = typeCount;

            }
            Departmenttd.RowSpan = departmentcount;
        }
    }

    public bool isHaveDoc(string DepartmentId)
    {
        var pp = (from cc in db.BaseDocs where cc.CreateDate.Value.Year == Convert.ToInt32(DptYear.SelectedValue) && cc.CreateDate.Value.Month == Convert.ToInt32(DptMonth.SelectedValue) && cc.SetDepartment == DepartmentId select cc).ToList();
        if (pp.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    protected void DptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("DocReporter.aspx?year=" + DptYear.SelectedValue + "&month=" + DptMonth.SelectedValue);
    }
    protected void DptYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("DocReporter.aspx?year=" + DptYear.SelectedValue + "&month=" + DptMonth.SelectedValue);
    }
}
